package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class LinearItemDecoration extends BaseItemDecoration {
    protected int orientation;

    public LinearItemDecoration(RecyclerView rcyList, int divider) {
        this(rcyList, divider, 0);
    }

    public LinearItemDecoration(RecyclerView rcyList, int divider, int color) {
        super(rcyList, divider, color);
        init();
    }

    public LinearItemDecoration(RecyclerView rcyList, int divider, Drawable drawable) {
        super(rcyList, divider, drawable);
        init();
    }

    private void init() {
        if (rcyList != null && rcyList.getLayoutManager() != null && rcyList.getLayoutManager() instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) rcyList.getLayoutManager()).getOrientation();
        } else {
            orientation = LinearLayoutManager.VERTICAL;
        }
    }

    @Override
    void drawDivider(Canvas canvas) {
        if (rcyList == null) return;
        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;
        int childCount = rcyList.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = rcyList.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int viewType = 0;
            if (adapter != null) {
                viewType = adapter.getItemViewType(params.getViewAdapterPosition());
            }
            if (viewType == BaseAdapter.TYPE_ITEM) {
                if (orientation == LinearLayoutManager.VERTICAL) {
                    left = child.getLeft();
                    top = child.getBottom();
                    right = child.getRight();
                    bottom = top + divider;
                } else {
                    left = child.getRight();
                    top = child.getTop();
                    right = left + divider;
                    bottom = child.getBottom();
                }
                if (drawable != null) {
                    drawable.setBounds(left, top, right, bottom);
                    drawable.draw(canvas);
                } else if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }

    @Override
    void configureItemOutRect(Rect outRect, View view) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
        int viewAdapterPosition = params.getViewAdapterPosition();
        if (adapter != null && adapter.getItemViewType(viewAdapterPosition) == BaseAdapter.TYPE_ITEM) {
            int position = adapter.getCurPosition(viewAdapterPosition);
            int left = 0;
            int top = 0;
            int right = 0;
            int bottom = 0;
            if (orientation == LinearLayoutManager.VERTICAL) {
                left = paddingLeft;
                if (paddingTop > 0 && position == 0) {
                    top = paddingTop;
                }
                right = paddingRight;
                if (paddingBottom > 0 && position == adapter.getDataCount() - 1) { //最后一条留白
                    bottom = paddingBottom + divider;
                } else {
                    bottom = divider;
                }
            } else {
                if (paddingLeft > 0 && position == 0) {
                    left = paddingLeft;
                }
                top = paddingTop;
                right = divider;
                bottom = paddingBottom;
            }
            outRect.set(left, top, right, bottom);
        } else {
            outRect.set(0, 0, 0, 0);
        }
    }
}
